package com.lczyfz.istep.modules.openstack.api.neutron.impl.securitygroup;

import com.lczyfz.istep.modules.openstack.api.neutron.impl.parseJson.NeutronParse;
import com.lczyfz.istep.modules.openstack.api.neutron.interfaces.securitygroup.Securitygroups;
import com.lczyfz.istep.modules.openstack.api.neutron.model.securitygroup.Securitygroup;
import com.lczyfz.istep.modules.openstack.constvary.ConstParams;
import com.lczyfz.istep.modules.openstack.constvary.RequestType;
import com.lczyfz.istep.modules.openstack.utils.HTTP;
import com.lczyfz.istep.modules.openstack.utils.Request;
import com.lczyfz.istep.modules.openstack.utils.RequestFactory;
import com.lczyfz.istep.modules.openstack.utils.Response;
import net.sf.json.JSONObject;

import java.util.List;

public class SecuritygroupImpl implements Securitygroups {
	public RequestFactory factory = new RequestFactory("neutron");
	
	@Override
	public List<Securitygroup> getSecuritygroups() {
		List<Securitygroup> securitygroups = null;
		Request request = factory.getRequest();
		request.setUrl("/v2.0/security-groups");
		request.setRequestType(RequestType.GET);
		
		Response results = HTTP.getConnectionResult(request);
		JSONObject body = results.getResponseBody();
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}	
		
		securitygroups = NeutronParse.resolveSecuritygroups(body);
		return securitygroups;
	}

	@Override
	public Securitygroup getSecuritygroupDetail(String securitygroupId) {
		Securitygroup securitygroup = new Securitygroup();
		Request request = factory.getRequest();
		request.setUrl("/v2.0/security-groups/" + securitygroupId);
		request.setRequestType(RequestType.GET);
		
		Response results = HTTP.getConnectionResult(request);
		JSONObject body = results.getResponseBody();
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		
		securitygroup = NeutronParse.resolveSecuritygroupDetail(body);
		return securitygroup;
	}

	@Override
	public Securitygroup createSecuritygroup(Securitygroup securitygroupinput) {
		Securitygroup securitygroup = new Securitygroup();
		
		JSONObject params = NeutronParse.packageSecuritygroup(securitygroupinput);
		System.out.println(params.toString());
		Request request = factory.getRequest();
		request.setUrl("/v2.0/security-groups");
		request.setRequestType(RequestType.POST);
		request.setBody(params);
		
		Response results = HTTP.getConnectionResult(request);
		if( ConstParams.RESPONSE_CODE_CREATED!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		System.out.println("==="+res);
		System.out.println("===");
		securitygroup = NeutronParse.resolveSecuritygroupDetail(res);
		return securitygroup;
	}

	@Override
	public void deleteSecuritygroup(String securitygroupId) {
		Request request = factory.getRequest();
		request.setUrl("/v2.0/security-groups/" + securitygroupId);
		request.setRequestType(RequestType.DELETE);
		
		Response results = HTTP.getConnectionResult(request);
		if(ConstParams.RESPONSE_CODE_SUCCESS  != results.getResponseCode() )
		{
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		return;
		
	}

	@Override
	public Securitygroup updateSecuritygroup(String securitygroupId, Securitygroup securitygroupupdateinput) {
		Securitygroup securitygroup = new Securitygroup();
		
		JSONObject params = NeutronParse.packageSecuritygroup(securitygroupupdateinput);
	//	System.out.println(params.toString());
		Request request = factory.getRequest();
		request.setUrl("/v2.0/security-groups/" + securitygroupId);
		request.setRequestType(RequestType.PUT);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);	
		
		if( ConstParams.RESPONSE_CODE_NORMAL!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		
		securitygroup = NeutronParse.resolveSecuritygroupDetail(res);
		return securitygroup;
	}
	
	public boolean checkUniqueName(String securitygroupName){
		return true;
	}
	
	public static void main(String[] args){
		SecuritygroupImpl sgtest = new SecuritygroupImpl();
//		System.out.println("��ȫ����Ϣ");
//		List<Securitygroup> sglist = sgtest.getSecuritygroups();
//		for(Securitygroup sg : sglist){
//			System.out.println(sg.getSecuritygroupId()+" "+sg.getsecuritygroupName() + " "+sg.getSecuritygroupProjectId()+" "+sg.getSecuritygroupTenantId()+" "+sg.getsecuritygroupSr());
//		}
//		
//		System.out.println("��ȫ����ϸ��Ϣ");
//		Securitygroup sg = sgtest.getSecuritygroupDetail("11878ec7-290a-4e1f-815b-188ffe8f605b");
//		System.out.println(sg.getSecuritygroupId()+" "+sg.getsecuritygroupName() + " "+sg.getSecuritygroupProjectId()+" "+sg.getSecuritygroupTenantId()+" "+sg.getsecuritygroupSr());
//	
		System.out.println("������ȫ��");
		Securitygroup sgtt = new Securitygroup();
		sgtt.setSecuritygroupName("sg04");
		Securitygroup sg_new = sgtest.createSecuritygroup(sgtt);
		System.out.println(sg_new.getSecuritygroupId()+" "+sg_new.getsecuritygroupName() + " "+sg_new.getSecuritygroupProjectId()+" "+sg_new.getSecuritygroupTenantId()+" "+sg_new.getsecuritygroupSr());
	
	/*	
		System.out.println("������ȫ��");
		Securitygroup sgtt = new Securitygroup();
		sgtt.setSecuritygroupName("sg01");
	
		Securitygroup sg_new = sgtest.createSecuritygroup(sgtt);
		System.out.println(sg_new.getSecuritygroupId()+" "+sg_new.getsecuritygroupName() + " "+sg_new.getSecuritygroupProjectId()+" "+sg_new.getSecuritygroupTenantId()+" "+sg_new.getsecuritygroupSr());
	*/
	//	System.out.println("ɾ����ȫ��");
	//	sgtest.deleteSecuritygroup("abd6f364-6e7f-4e80-9a64-50c564ce2204");
		
	/*	System.out.println("���°�ȫ��");
		
		Securitygroup sgttupdate = new Securitygroup();
		sgttupdate.setSecuritygroupName("sg00");
		Securitygroup sg_update = sgtest.updateSecuritygroup("abd6f364-6e7f-4e80-9a64-50c564ce2204", sgttupdate);
		System.out.println(sg_update.getSecuritygroupId()+" "+sg_update.getsecuritygroupName() + " "+sg_update.getSecuritygroupProjectId()+" "+sg_update.getSecuritygroupTenantId()+" "+sg_update.getsecuritygroupSr());
	*/
	}

}
